/*

Use as:

import org.costas.classes.*;

private function onLoadFileClick():void
{
mdiCanvas.windowManager.removeAll();
var localXML:LoadNetxml = new LoadNetxml();
localXML.addEventListener(LoadNetxmlEvent.LOADED, onLocalXMLLoaded);
localXML.addEventListener(LoadNetxmlEvent.CANCELED, onLocalXMLCanceled);
localXML.LoadFile();
} // function onLoadFileClick

private function onLocalXMLLoaded(evt:LoadNetxmlEvent):void {
kismetXML = evt.data as XML;
var kismetdata:ParserNetworksList = new ParserNetworksList(evt.data as XML);
kismetdata.addEventListener(ParserNetworksListEvent.PARSED, onNetworksListParsed);
kismetdata.addEventListener(ParserNetworksListEvent.ERROR, onNetworksListError);
kismetdata.Parse();
} // function onLocalXMLLoaded

private function onNetworksListParsed(evt:ParserNetworksListEvent):void {
kismetnetworksList = evt.data as ArrayCollection;
btnGps.enabled = true;
btnDetails.enabled = true;
} // function onNetworksListParsed

private function onNetworksListError(evt:ParserNetworksListEvent):void {
Alert.show("Maybe old version ?", "Parsing Error");
kismetnetworksList = null;
btnGps.enabled = false;
btnDetails.enabled = false;
} // function onNetworksListError

*/
package org.costas.classes
{
	import flash.events.*;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	
	import mx.controls.Alert;
	import mx.rpc.events.XMLLoadEvent;
	
	import org.costas.classes.LoadNetxmlEvent;
	
	[Event(name="LOADED", type="LoadNetxmlEvent")]
	[Event(name="CANCELED", type="LoadNetxmlEvent")]
	[Event(name="ERROR",  type="LoadNetxmlEvent")]	
	
	public class LoadNetxml extends EventDispatcher
	{
		public var data:Object = new Object();
		
		//FileReference Class well will use to load data
		private var fr:FileReference;
		
		//File types which we want the user to open
		private static const FILE_TYPES:Array = [new FileFilter("Kismet File", "*.netxml;*.xml")];
		
		public function LoadNetxml(target:IEventDispatcher=null)
		{
			super(target);
			
			//create the FileReference instance
			fr = new FileReference();
			
			//listen for when they select a file
			fr.addEventListener(Event.SELECT, onFileSelect);
			
			//listen for when then cancel out of the browse dialog
			fr.addEventListener(Event.CANCEL,onCancel);
		} // function LoadLocalXML
		
		public function LoadFile():void {
			
			//open a native browse dialog that filters for text files
			fr.browse(FILE_TYPES);			
		}
		
		/************ Browse Event Handlers **************/
		
		//called when the user selects a file from the browse dialog
		private function onFileSelect(e:Event):void
		{
			//listen for when the file has loaded
			fr.addEventListener(Event.COMPLETE, onLoadComplete);
			
			//listen for any errors reading the file
			fr.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
			
			//load the content of the file
			fr.load();
		} //function onFileSelect
		
		//called when the user cancels out of the browser dialog
		private function onCancel(e:Event):void
		{
			dispatchEvent(new LoadNetxmlEvent(LoadNetxmlEvent.CANCELED));
			trace("File Browse Canceled");
			fr = null;
		}
		
		/************ Select Event Handlers **************/
		
		//called when the file has completed loading
		private function onLoadComplete(e:Event):void
		{
			//get the data from the file as a ByteArray
			var data:ByteArray = fr.data;
			
			//read the bytes of the file as a string and put it in a XMLDocument
			var xmlstring:String = data.readUTFBytes(data.bytesAvailable).toString();
			var xmldata:XML = new XML(xmlstring);
			
			//validate valid netxml document
			var version:String = xmldata["@kismet-version"];
			version = version.substr(0,4);
			if (Number(version) < 2010) {
				Alert.show("Invalid netxml file", "File Error");
			} else {
				//Data are ready to be send
				this.data = xmldata;
				dispatchEvent(new LoadNetxmlEvent(LoadNetxmlEvent.LOADED, this.data));				
			}
			
			//clean up the FileReference instance
			fr = null;
		} // function onLoadComplete
		
		//called if an error occurs while loading the file contents
		private function onLoadError(e:IOErrorEvent):void
		{
			//clean up the FileReference instance
			fr = null;
			dispatchEvent(new LoadNetxmlEvent(LoadNetxmlEvent.ERROR, e.text));
			Alert.show(e.text, "Error loading file");
			trace("Error loading file : " + e.text);
		}
	} //class LoadLocalXML
}